package com.cms.gdm.service.impl;

import com.alibaba.fastjson.TypeReference;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cms.common.utils.R;
import com.cms.gdm.dao.TeacherDao;
import com.cms.gdm.entity.TeacherEntity;
import com.cms.gdm.entity.vo.GuideVo;
import com.cms.gdm.feign.StuFeign;
import com.cms.gdm.to.StudentTo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cms.common.utils.PageUtils;
import com.cms.common.utils.Query;

import com.cms.gdm.dao.GuideDao;
import com.cms.gdm.entity.GuideEntity;
import com.cms.gdm.service.GuideService;

import javax.annotation.Resource;


@Service("guideService")
public class GuideServiceImpl extends ServiceImpl<GuideDao, GuideEntity> implements GuideService {

    @Autowired
    private StuFeign stuFeign;

    @Resource
    private TeacherDao teacherDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        String key = (String) params.get("key");
        QueryWrapper<GuideEntity> wrapper = new QueryWrapper<>();
        if(!StringUtils.isEmpty(key)){
            wrapper.eq("id", key);
        }
        IPage<GuideEntity> page = this.page(
                new Query<GuideEntity>().getPage(params),
                wrapper
        );

        List<GuideVo> collect = page.getRecords().stream().map(item -> {
            GuideVo guideVo = new GuideVo();
            BeanUtils.copyProperties(item, guideVo);
            Long studentId = item.getStudentId();
            R r = stuFeign.info(studentId);
            if (r.getCode() == 0) {
                StudentTo student = r.getData("student", new TypeReference<StudentTo>() {
                });
                guideVo.setStudentName(student.getName());
            }
            TeacherEntity teacherEntity = teacherDao.selectById(item.getTeacherId());
            guideVo.setTeacherName(teacherEntity.getName());
            return guideVo;
        }).collect(Collectors.toList());

        IPage<GuideVo> resultPage = new Page<>();
        resultPage.setRecords(collect);
        resultPage.setCurrent(page.getCurrent());
        resultPage.setPages(page.getPages());
        resultPage.setSize(page.getSize());
        resultPage.setTotal(page.getTotal());

        return new PageUtils(resultPage);
    }

    @Override
    public GuideVo infoDetail(Long id) {
        GuideVo guideVo = new GuideVo();
        GuideEntity guideEntity = baseMapper.selectById(id);
        BeanUtils.copyProperties(guideEntity, guideVo);
        R r = stuFeign.info(guideEntity.getStudentId());
        if(r.getCode() == 0){
            StudentTo student = r.getData("student", new TypeReference<StudentTo>() {
            });
            guideVo.setStudentName(student.getName());
        }
        return guideVo;
    }

}